﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: OneSampleChiSquareTest.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-09-06 11:39 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using BOL.Maths.Distributions;

namespace BOL.Maths.Statistics.HypothesisTest
{
    public sealed class OneSampleChiSquareTest<T> : OneSampleTestBase<T>
        where T: IEquatable<T>
    {
        #region Private variables

        private readonly T[] _values; // distinct values
        private readonly int _degreesOfFreedom; // number of values - 1
        private readonly int[] _cObs;
        private readonly int[] _cExp;

        #endregion

        #region Public properties

        public new double TestStatistic { get { return _cObs.Zip(_cExp, (o, e) => (o - e) / e).Sum(); } }

        #endregion

        #region Constructors

        public OneSampleChiSquareTest(double alpha, IEnumerable<T> observed, IEnumerable<T> expected)
            : base(TestType.Upper, alpha, double.NaN, observed) 
        {
            if (observed == null)
                throw new ArgumentNullException("observed");

            if (expected == null)
                throw new ArgumentNullException("expected");

            var o = observed.ToArray();
            var e = expected.ToArray();

            _values = o.Distinct().ToArray();
            _degreesOfFreedom = _values.Length - 1;

            _cObs = new int[_degreesOfFreedom + 1];
            _cExp = new int[_degreesOfFreedom + 1];
            for (var i = 0; i <= _degreesOfFreedom; i++)
            {
                _cObs[i] = o.Select(x => x.Equals(_values[i])).Count();
                _cExp[i] = e.Select(x => x.Equals(_values[i])).Count();
            }

            Distribution = new ChiSquareDistribution(_degreesOfFreedom);
        }

        #endregion

        #region Protected methods

        protected override double PointEstimate()
        {
            throw new Exception("Point estimate is undefined for chi square test.");
        }

        protected override double PointEstimateStandardError()
        {
            throw new Exception("Point estimate standard error is undefined for chi square test.");
        }

        #endregion

        #region Object overriden

        public override string ToString()
        {
            return String.Format("Chi^2 Test (Chi^2 = {0}, p-value = {1:G3}): {2}", TestStatistic, PValue, (IsSignificant) ? "Reject H0" : "Accept H0");
        }

        #endregion
    }
}
